`
https://leetcode.cn/problems/diameter-of-binary-tree/
`

/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**
 * @param {TreeNode} root
 * @return {number}
 */
var diameterOfBinaryTree = function (root) {
  // 存储最长直径
  let curMaxDiameter = 0

  // 计算深度
  const depth = (root) => {
    if (!root) return 0
    const left = depth(root.left)
    const right = depth(root.right)
    // 更新以当前结点作为根节点的最长直径长度
    // 一颗树里的最长直径不一定经过根节点，也可能在根节点的左或右子树内部
    curMaxDiameter = Math.max(curMaxDiameter, left + right)
    return Math.max(left, right) + 1
  }

  // 计算最长直径
  depth(root)

  return curMaxDiameter
};